Ext.define('app.view.DataRange', {
	extend : 'Ext.form.FieldContainer',
	alias : 'widget.daterangefield',

	requires : ['Ext.form.field.Date', 'Ext.form.field.Time', 'Ext.form.Label',
			'Ext.form.field.Checkbox', 'Ext.layout.container.Column'],
	defaults : {
		margin : '0 5 0 0'
	},

	initComponent : function() {
		Ext.apply(this, {
			layout : 'hbox',
			xtype : 'container',
			items : [{
				xtype : 'datefield',
				itemId : this.id + '-start-date',
				allowBlank:false,
				format : 'Y/n/j',
				width : 80
			}, {
				xtype : 'timefield',
				itemId : this.id + '-start-time',
				allowBlank:false,
				labelWidth : 0,
				hideLabel : true,
				width : 60,
				format : 'G:i'
			}, {
				xtype : 'label',
				text : '-',
				margin : '4 5 0 0'
			}, {
				xtype : 'datefield',
				itemId : this.id + '-end-date',
				allowBlank:false,
				format : 'Y/n/j',
				hideLabel : true,
				width : 80
			}, {
				xtype : 'timefield',
				itemId : this.id + '-end-time',
				labelWidth : 0,
				allowBlank:false,
				hideLabel : true,
				width : 60,
				format : 'G:i'
			}]
		})
		this.addCls('ext-dt-range');
		this.callParent(arguments);
		this.initRefs();
	},
	initRefs : function() {
		var me = this;
		me.startDate = me.down('#' + me.id + '-start-date');
		me.startTime = me.down('#' + me.id + '-start-time');
		me.endTime = me.down('#' + me.id + '-end-time');
		me.endDate = me.down('#' + me.id + '-end-date');
		me.Valid_type = me.down('#' + me.id + 'Valid_type');
	},
	getFieldConfigs : function() {
		var me = this;
		return [me.getStartDateConfig(), me.getStartTimeConfig(),
				me.getDateSeparatorConfig(), me.getEndTimeConfig(),
				me.getEndDateConfig()];
	},

	getStartDateConfig : function() {
		return {
			xtype : 'datefield',
			itemId : this.id + '-start-date',
			format : 'Y/n/j',
			width : 100
		};
	},

	getStartTimeConfig : function() {
		return {
			xtype : 'timefield',
			itemId : this.id + '-start-time',
			labelWidth : 0,
			hideLabel : true,
			width : 90,
			format : 'G:i'
		};
	},
	updateState : function() {
		if (this["Valid_type"].getValue() == 1) {
			this["startDate"].setDisabled(true);
			this["startTime"].setDisabled(true);
			this["endTime"].setDisabled(true);
			this["endDate"].setDisabled(true);
			this["startDate"].setValue("");
			this["startTime"].setValue("");
			this["endTime"].setValue("");
			this["endDate"].setValue("");
		} else {
			this["startDate"].setDisabled(false);
			this["startTime"].setDisabled(false);
			this["endTime"].setDisabled(false);
			this["endDate"].setDisabled(false);
		}
	},
	getEndDateConfig : function() {
		return {
			xtype : 'datefield',
			itemId : this.id + '-end-date',
			format : 'Y/n/j',
			hideLabel : true,
			width : 100
		};
	},

	getEndTimeConfig : function() {
		return {
			xtype : 'timefield',
			itemId : this.id + '-end-time',
			labelWidth : 0,
			hideLabel : true,
			width : 90,
			format : 'G:i'
		};
	},
	getDateSeparatorConfig : function() {
		return {
			xtype : 'label',
			text : '-',
			margin : '4 5 0 0'
		};
	},
	getValid_type : function() {
		return this["Valid_type"].getValue();
	},
	setValid_type : function(v) {
		return this["Valid_type"].setValue(v);
	},
	getStartDate : function() {
		return this.getDT('start');
	},
	getEndDate : function() {
		return this.getDT('end');
	},
	getDT : function(startend) {
		var time = this[startend + 'Time'].getValue(), dt = this[startend
				+ 'Date'].getValue();

		if (Ext.isDate(dt)) {
			dt = Ext.Date.format(dt, this[startend + 'Date'].format);
		} else {
			return null;
		}
		if (time && time !== '') {
			time = Ext.Date.format(time, this[startend + 'Time'].format);
			var val = Ext.Date.parseDate(dt + ' ' + time, this[startend
							+ 'Date'].format
							+ ' ' + this[startend + 'Time'].format);
			return val;
			// return Ext.Date.parseDate(dt+' '+time,
			// this[startend+'Date'].format+' '+this[startend+'Time'].format);
		}
		return Ext.Date.parseDate(dt, this[startend + 'Date'].format);

	},
	setStartDate : function(v) {
		this.setDT(v, 'start');
	},
	setEndDate : function(v) {
		this.setDT(v, 'end');
	},
	// private setValue helper
	setDT : function(dt, startend) {
		if (dt && Ext.isDate(dt)) {
			//dt = new Date(dt.valueOf() - 60 * 60 * 1000);
			this[startend + 'Date'].setValue(dt);
			this[startend + 'Time'].setValue(Ext.Date.format(dt, this[startend
							+ 'Time'].format));
			return true;
		}
	},

	// inherited docs
	isDirty : function() {
		var dirty = false;
		if (this.rendered && !this.disabled) {
			this.items.each(function(item) {
						if (item.isDirty()) {
							dirty = true;
							return false;
						}
					});
		}
		return dirty;
	},

	// private
	onDisable : function() {
		this.delegateFn('disable');
	},

	// private
	onEnable : function() {
		this.delegateFn('enable');
	},

	// inherited docs
	reset : function() {
		this.delegateFn('reset');
	},

	// private
	delegateFn : function(fn) {
		this.items.each(function(item) {
					if (item[fn]) {
						item[fn]();
					}
				});
	},

	// private
	beforeDestroy : function() {
		Ext.destroy(this.fieldCt);
		this.callParent(arguments);
	},

	getRawValue : Ext.emptyFn,
	/**
	 * @method setRawValue
	 * @hide
	 */
	setRawValue : Ext.emptyFn
});